<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="钉钉代办任务ID" prop="dingtalkTaskId">
        <el-input
            v-model="queryParams.dingtalkTaskId"
            placeholder="请输入钉钉代办任务ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="钉钉消息ID" prop="dingtalkMsgId">
        <el-input
            v-model="queryParams.dingtalkMsgId"
            placeholder="请输入钉钉消息ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程定义" prop="processDefinitionId">
        <el-input
            v-model="queryParams.processDefinitionId"
            placeholder="请输入流程定义"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程实例ID" prop="processInstancesId">
        <el-input
            v-model="queryParams.processInstancesId"
            placeholder="请输入流程实例ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程节点" prop="processDesignId">
        <el-input
            v-model="queryParams.processDesignId"
            placeholder="请输入流程节点"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理人" prop="APPROVER">
        <el-input
            v-model="queryParams.APPROVER"
            placeholder="请输入处理人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原始处理人" prop="originalApprover">
        <el-input
            v-model="queryParams.originalApprover"
            placeholder="请输入原始处理人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转交人" prop="TRANSFEROR">
        <el-input
            v-model="queryParams.TRANSFEROR"
            placeholder="请输入转交人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="前一个任务" prop="preTaskId">
        <el-input
            v-model="queryParams.preTaskId"
            placeholder="请输入前一个任务"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来自那个任务退回的" prop="fromBackTaskId">
        <el-input
            v-model="queryParams.fromBackTaskId"
            placeholder="请输入来自那个任务退回的"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来自那个任务转交的" prop="fromTransferTaskId">
        <el-input
            v-model="queryParams.fromTransferTaskId"
            placeholder="请输入来自那个任务转交的"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来自那个任务知会的" prop="fromInformedTaskId">
        <el-input
            v-model="queryParams.fromInformedTaskId"
            placeholder="请输入来自那个任务知会的"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来自那个任务撤回的" prop="fromWithdrawTaskId">
        <el-input
            v-model="queryParams.fromWithdrawTaskId"
            placeholder="请输入来自那个任务撤回的"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批方式" prop="approvalMethod">
        <el-input
            v-model="queryParams.approvalMethod"
            placeholder="请输入审批方式"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程知会" prop="INFORMED">
        <el-input
            v-model="queryParams.INFORMED"
            placeholder="请输入流程知会"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程传阅" prop="CIRCULATE">
        <el-input
            v-model="queryParams.CIRCULATE"
            placeholder="请输入流程传阅"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否已读" prop="READ">
        <el-input
            v-model="queryParams.READ"
            placeholder="请输入是否已读"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否重新开始" prop="RESTART">
        <el-input
            v-model="queryParams.RESTART"
            placeholder="请输入是否重新开始"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否重定位" prop="RELOCATION">
        <el-input
            v-model="queryParams.RELOCATION"
            placeholder="请输入是否重定位"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否自动审批" prop="AUTO">
        <el-input
            v-model="queryParams.AUTO"
            placeholder="请输入是否自动审批"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="捎话" prop="takeMsg">
        <el-input
            v-model="queryParams.takeMsg"
            placeholder="请输入捎话"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
            v-model="queryParams.createBy"
            placeholder="请输入创建人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy">
        <el-input
            v-model="queryParams.updateBy"
            placeholder="请输入更新人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker clearable
                        v-model="queryParams.updateTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['flow:task:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['flow:task:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['flow:task:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['flow:task:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table header-cell-class-name="custom-header-cell" v-loading="loading" :data="taskList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="任务ID" align="center" prop="id"/>
      <el-table-column label="流程定义" align="center" prop="processDefinitionId"/>
      <el-table-column label="流程实例ID" align="center" prop="processInstancesId"/>
      <el-table-column label="流程节点" align="center" prop="processDesignId"/>
      <TaskStatusColumn/>
      <el-table-column label="任务类型 1 发起人任务 2 审批人任务 3 抄送人任务 4 转交人任务" align="center"
                       prop="taskType">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.taskType == G.TASK_TYPE.INITIATOR_TASK" size="small">
            {{ G.TASK_TYPE_NAME.INITIATOR_TASK }}
          </el-tag>
          <el-tag type="warning" v-if="scope.row.taskType == G.TASK_TYPE.APPROVER_TASK" size="small">
            {{ G.TASK_TYPE_NAME.APPROVER_TASK }}
          </el-tag>
          <el-tag type="info" v-if="scope.row.taskType == G.TASK_TYPE.CC_TASK" size="small">
            {{ G.TASK_TYPE_NAME.CC_TASK }}
          </el-tag>
          <el-tag type="info" v-if="scope.row.taskType == G.TASK_TYPE.TRANSFER_TASK" size="small">
            {{ G.TASK_TYPE_NAME.TRANSFER_TASK }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="当前处理人" align="center" prop="approverName"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{
              parseTime(scope.row.createTime,
                  '{y}-{m}-{d}')
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{
              parseTime(scope.row.updateTime,
                  '{y}-{m}-{d}')
            }}</span>
        </template>
      </el-table-column>
      <el-table-column width="150" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['flow:task:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['flow:task:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!-- 添加或修改流程任务对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="taskRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="钉钉代办任务ID" prop="dingtalkTaskId">
          <el-input v-model="form.dingtalkTaskId" placeholder="请输入钉钉代办任务ID"/>
        </el-form-item>
        <el-form-item label="钉钉消息ID" prop="dingtalkMsgId">
          <el-input v-model="form.dingtalkMsgId" placeholder="请输入钉钉消息ID"/>
        </el-form-item>
        <el-form-item label="流程定义" prop="processDefinitionId">
          <el-input v-model="form.processDefinitionId" placeholder="请输入流程定义"/>
        </el-form-item>
        <el-form-item label="流程实例ID" prop="processInstancesId">
          <el-input v-model="form.processInstancesId" placeholder="请输入流程实例ID"/>
        </el-form-item>
        <el-form-item label="流程节点" prop="processDesignId">
          <el-input v-model="form.processDesignId" placeholder="请输入流程节点"/>
        </el-form-item>
        <el-form-item label="处理人" prop="APPROVER">
          <el-input v-model="form.APPROVER" placeholder="请输入处理人"/>
        </el-form-item>
        <el-form-item label="原始处理人" prop="originalApprover">
          <el-input v-model="form.originalApprover" placeholder="请输入原始处理人"/>
        </el-form-item>
        <el-form-item label="转交人" prop="TRANSFEROR">
          <el-input v-model="form.TRANSFEROR" placeholder="请输入转交人"/>
        </el-form-item>
        <el-form-item label="前一个任务" prop="preTaskId">
          <el-input v-model="form.preTaskId" placeholder="请输入前一个任务"/>
        </el-form-item>
        <el-form-item label="来自那个任务退回的" prop="fromBackTaskId">
          <el-input v-model="form.fromBackTaskId" placeholder="请输入来自那个任务退回的"/>
        </el-form-item>
        <el-form-item label="来自那个任务转交的" prop="fromTransferTaskId">
          <el-input v-model="form.fromTransferTaskId" placeholder="请输入来自那个任务转交的"/>
        </el-form-item>
        <el-form-item label="来自那个任务知会的" prop="fromInformedTaskId">
          <el-input v-model="form.fromInformedTaskId" placeholder="请输入来自那个任务知会的"/>
        </el-form-item>
        <el-form-item label="来自那个任务撤回的" prop="fromWithdrawTaskId">
          <el-input v-model="form.fromWithdrawTaskId" placeholder="请输入来自那个任务撤回的"/>
        </el-form-item>
        <el-form-item label="审批方式" prop="approvalMethod">
          <el-input v-model="form.approvalMethod" placeholder="请输入审批方式"/>
        </el-form-item>
        <el-form-item label="流程知会" prop="INFORMED">
          <el-input v-model="form.INFORMED" placeholder="请输入流程知会"/>
        </el-form-item>
        <el-form-item label="流程传阅" prop="CIRCULATE">
          <el-input v-model="form.CIRCULATE" placeholder="请输入流程传阅"/>
        </el-form-item>
        <el-form-item label="是否已读" prop="READ">
          <el-input v-model="form.READ" placeholder="请输入是否已读"/>
        </el-form-item>
        <el-form-item label="是否重新开始" prop="RESTART">
          <el-input v-model="form.RESTART" placeholder="请输入是否重新开始"/>
        </el-form-item>
        <el-form-item label="是否重定位" prop="RELOCATION">
          <el-input v-model="form.RELOCATION" placeholder="请输入是否重定位"/>
        </el-form-item>
        <el-form-item label="是否自动审批" prop="AUTO">
          <el-input v-model="form.AUTO" placeholder="请输入是否自动审批"/>
        </el-form-item>
        <el-form-item label="捎话" prop="takeMsg">
          <el-input v-model="form.takeMsg" placeholder="请输入捎话"/>
        </el-form-item>
        <el-form-item label="审批意见" prop="REMARK">
          <el-input v-model="form.REMARK" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Task">
import {
  listTask,
  getTask,
  delTask,
  addTask,
  updateTask
} from "@/api/flow/task";
import moment from "moment";
import TaskStatusColumn from "@/components/EbComponent/TableColumn/TaskStatusColumn.vue";
const {proxy} = getCurrentInstance();
const taskList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dingtalkTaskId: null,
    dingtalkMsgId: null,
    processDefinitionId: null,
    processInstancesId: null,
    processDesignId: null,
    APPROVER: null,
    originalApprover: null,
    TRANSFEROR: null,
    preTaskId: null,
    fromBackTaskId: null,
    fromTransferTaskId: null,
    fromInformedTaskId: null,
    fromWithdrawTaskId: null,
    taskType: null,
    taskStatus: null,
    approvalMethod: null,
    INFORMED: null,
    CIRCULATE: null,
    READ: null,
    RESTART: null,
    RELOCATION: null,
    AUTO: null,
    takeMsg: null,
    REMARK: null,
    createBy: null,
    createTime: moment()
        .format('YYYY-MM-DD'),
    updateBy: null,
    updateTime: null
  },
  rules: {}
});
const {
  queryParams,
  form,
  rules
} = toRefs(data);
/** 查询流程任务列表 */
function getList() {
  loading.value = true;
  listTask(queryParams.value)
      .then(response => {
        taskList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    ID: null,
    dingtalkTaskId: null,
    dingtalkMsgId: null,
    processDefinitionId: null,
    processInstancesId: null,
    processDesignId: null,
    APPROVER: null,
    originalApprover: null,
    TRANSFEROR: null,
    preTaskId: null,
    fromBackTaskId: null,
    fromTransferTaskId: null,
    fromInformedTaskId: null,
    fromWithdrawTaskId: null,
    taskType: null,
    taskStatus: null,
    approvalMethod: null,
    INFORMED: null,
    CIRCULATE: null,
    READ: null,
    RESTART: null,
    RELOCATION: null,
    AUTO: null,
    takeMsg: null,
    REMARK: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("taskRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.ID);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加流程任务";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _ID = row.ID || ids.value
  getTask(_ID)
      .then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改流程任务";
      });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["taskRef"].validate(valid => {
    if (valid) {
      if (form.value.ID != null) {
        updateTask(form.value)
            .then(response => {
              proxy.$modal.msgSuccess("修改成功");
              open.value = false;
              getList();
            });
      } else {
        addTask(form.value)
            .then(response => {
              proxy.$modal.msgSuccess("新增成功");
              open.value = false;
              getList();
            });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _IDs = row.ID || ids.value;
  proxy.$modal.confirm('是否确认删除流程任务编号为"' + _IDs + '"的数据项？')
      .then(function () {
        return delTask(_IDs);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('flow/task/export',
      {
        ...queryParams.value
      },
      `task_${new Date().getTime()}.xlsx`)
}
getList();
</script>
<style scoped lang="scss">
:deep(.custom-header-cell) {
  vertical-align: top;

  .cell {
  }
}
</style>
